﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net.Mail;
using HCFramework.API.LINQ;

namespace HCFramework.User.ForgetPassword
{
    public partial class ForgetPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            API.LINQ.User user = C.UserController.Get(txtUsername.Text, txtEmail.Text);
            if (user == null)
            {
                M.Message.ShowError("User không tồn tại");
                return;
            }

            string newPass = Guid.NewGuid().ToString().Split('-').First();
            HCDataContext db = new HCDataContext();
            API.LINQ.User u = C.UserController.GetByID(user.UserID, db);
            u.Password = C.UserController.EncryptPassword(newPass);
            C.UserController.Update(u, db);

            string subject = M.FileM.Read(@"\User\ForgetPassword\Template\EmailSubject.htm");
            string body = M.FileM.Read(@"\User\ForgetPassword\Template\EmailBody.htm");
            body = M.Template.MergeUser(body, user);
            body = body.Replace("{NewPassword}", newPass);

            MailMessage mail = new MailMessage();
            mail.Subject = subject;
            mail.Body = body;
            mail.To.Add(txtEmail.Text);
            M.Common.SendEmail(mail);

            M.Message.ShowSuccess("Mật khẩu đã được gửi vào " + txtEmail.Text);
        }
    }
}